package com.solotiger.geographic.ui.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

import com.solotiger.geographic.R;
import com.solotiger.geographic.util.SystemUtil;

import java.util.Random;

/**
 * 闪屏页面
 */
public class SplashActivity extends Activity {

    private int[] images = {
            R.drawable.splash,
            R.drawable.splash_1,
            R.drawable.splash_2
    };

    private ImageView mIvBackground;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(null);
        setContentView(R.layout.activity_splash);
        mIvBackground = findViewById(R.id.image_bg);
        int i = new Random().nextInt(images.length);
        mIvBackground.setImageResource(images[i]);

        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mIvBackground, "alpha", 1f, 0f);
        objectAnimator.setDuration(3000);
        objectAnimator.setInterpolator(new DecelerateInterpolator());
        objectAnimator.setStartDelay(1000);
        objectAnimator.start();
        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                SystemUtil.startActivity(SplashActivity.this, intent);
                finish();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

    }
}
